package cn.tedu.store.search.webapi.controller;

import cn.tedu.store.commons.pojo.search.entity.EsProduct;
import cn.tedu.store.commons.web.JsonPage;
import cn.tedu.store.commons.web.JsonResult;
import cn.tedu.store.search.service.IEsProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 全文搜索控制器
 *
 * @author RelentlessFlow
 * @version 0.0.1
 */
@Slf4j
@Api(tags = "商品搜索管理模块")
@RestController
@RequestMapping("/search")
public class EsProductController {

    @Autowired
    private IEsProductService esProductService;

    @ApiOperation(value = "导入所有数据库中商品到ES")
    @PostMapping(value = "/import-all")
    public JsonResult<Integer> importAllList() {
        int count = esProductService.importAll();
        return JsonResult.ok(count);
    }

    @ApiOperation(value = "根据id删除商品")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @PostMapping(value = "/{id:[0-9]+}/delete")
    @ResponseBody
    public JsonResult<Void> delete(@PathVariable Long id) {
        esProductService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation(value = "根据id批量删除商品")
    @PostMapping(value = "/delete/batch")
    public JsonResult<Void> delete(List<Long> ids) {
        esProductService.delete(ids);
        return JsonResult.ok();
    }

    @ApiOperation(value = "根据id创建商品")
    @PostMapping(value = "/{id:[0-9]+}/create")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @ResponseBody
    public JsonResult<EsProduct> create(@PathVariable Long id) {
        EsProduct esProduct = esProductService.create(id);
        return JsonResult.ok(esProduct);
    }

    @ApiOperation(value = "简单搜索")
    @GetMapping(value = "/search/simple")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键词"),
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "0"),
            @ApiImplicitParam(name = "pageSize", value = "单页条数", defaultValue = "5")
    })
    public JsonResult<JsonPage<EsProduct>> search(String keyword, Integer pageNum, Integer pageSize) {
        log.info("keyword: {}",keyword);
        JsonPage<EsProduct> esProductPage = esProductService.searchSimple(keyword, pageNum, pageSize);
        return JsonResult.ok(esProductPage);
    }

    @ApiOperation(value = "条件筛选搜索")
    @GetMapping(value = "/search/filter")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键词", defaultValue = ""),
            @ApiImplicitParam(name = "brandId", value = "品牌id"),
            @ApiImplicitParam(name = "categoryId", value = "分类id"),
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "0"),
            @ApiImplicitParam(name = "pageSize", value = "单页条数", defaultValue = "5"),
            @ApiImplicitParam(name = "sort", value = "排序方式 1->新旧 2->销量 3->价格升序 4->价格降序 其它->相关度", defaultValue = "5"),
    })
    public JsonResult<JsonPage<EsProduct>> search(String keyword, Long brandId, Long categoryId, Integer pageNum, Integer pageSize, Integer sort) {
        JsonPage<EsProduct> esProductPage = esProductService.search(keyword, brandId, categoryId, pageNum, pageSize, sort);
        return JsonResult.ok(esProductPage);
    }
}
